﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.IO;
using System.Xml;
using System.Xml.Serialization;
using System.Windows;

namespace dkProfiler
{
    [XmlRootAttribute(ElementName = "Profile", IsNullable = false)]
    public class Profile : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;

        protected void OnPropertyChanged(string propertyName)
        {
            PropertyChangedEventHandler handler = PropertyChanged;
            if (handler != null)
                handler(this, new PropertyChangedEventArgs(propertyName));
        }

        public string Name { get; set; }

        [XmlIgnore()]
        public int Level { get; set; } // not quite there with level specific stat calculations, so not included for now

        private int race;
        public int Race
        {
            get { return race; }
            set
            {
                race = value;
                Stats.Recalculate();
                OnPropertyChanged(null);
            }
        }

        // Some logic to make sure you can't pick the same profession twice
        private int profession1;
        public int Profession1
        {
            get { return profession1; }
            set
            {
                profession1 = value;
                if (profession1 == profession2 && profession2 > 0)
                {
                    profession2 = 0;
                    OnPropertyChanged("Profession2");
                }
            }
        }
        private int profession2;
        public int Profession2
        {
            get { return profession2; }
            set
            {
                profession2 = value;
                if (profession2 == profession1 && profession1 > 0)
                {
                    profession1 = 0;
                    OnPropertyChanged("Profession1");
                }
            }
        }

        public int Presence { get; set; }
        public Equipment Equipment { get; set; }

        [XmlIgnore()]
        public Stats Stats { get; set; }

        /// <summary>
        /// Constructor
        /// </summary>
        public Profile()
        {
            this.Stats = new Stats(this);
            this.Name = "Unnamed profile";
            this.Level = 80;
            this.Equipment = new Equipment();
        }

        /// <summary>
        /// Logic for loading a profile from a file
        /// </summary>
        public static Profile LoadFromFile(string fileName)
        {
            XmlSerializer xs = new XmlSerializer(typeof(Profile));
            try
            {
                Profile profile;
                using (FileStream fs = new FileStream(fileName, FileMode.Open))
                    profile = xs.Deserialize(fs) as Profile;

                return profile;
            }
            catch (Exception e)
            {
                if (e is InvalidOperationException)
                    MessageBox.Show("The loaded file is not a valid profile file.", "Invalid profile file", MessageBoxButton.OK, MessageBoxImage.Error);

                return null;
            }
        }

        /// <summary>
        /// Logic for saving a profile to a file
        /// </summary>
        public static void SaveToFile(Profile profile, string fileName)
        {
            XmlSerializer xs = new XmlSerializer(typeof(Profile));
            using (FileStream fs = new FileStream(fileName, FileMode.Create))
                xs.Serialize(fs, profile);
        }
    }
}
